package com.dongdongshop.controller;

import com.alibaba.fastjson.JSONObject;
import com.dongdongshop.data.Result;
import com.dongdongshop.pojo.TbSpecificationOption;
import com.dongdongshop.pojo.TbTypeTemplate;
import com.dongdongshop.service.SpecOptionService;
import com.dongdongshop.service.TypeTemplateService;
import com.dongdongshop.vo.TbTypeTemplateVO;
import com.dongdongshop.vo.TypeSpecificationVO;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("temTemplate")
public class TypeTemplateController {

    @DubboReference
    private TypeTemplateService templateService;

    @DubboReference
    private SpecOptionService specOptionService;

    @RequestMapping("selectByTypeId")
    @ResponseBody
    public Result selectByTypeId(Long id){
        TbTypeTemplate template = templateService.selectById(id);

        //从模板中取出规格
        String specIds = template.getSpecIds();

        //class转换的时候，有值就加，没值就会为空
        List<TypeSpecificationVO> voList = JSONObject.parseArray(specIds, TypeSpecificationVO.class);

        for (TypeSpecificationVO vo : voList) {
            List<TbSpecificationOption> optionList = specOptionService.selectBySpecId(vo.getId());
            vo.setOptionList(optionList);
        }

        TbTypeTemplateVO templateVO = new TbTypeTemplateVO();
        templateVO.setId(template.getId());
        templateVO.setBrandIds(template.getBrandIds());
        templateVO.setCustomAttributeItems(template.getCustomAttributeItems());
        templateVO.setName(template.getName());
        templateVO.setSpecIds(voList);

        return Result.ok().setData(templateVO);
    }
}
